/**
 * Created by zyy on 15/5/5.
 * zhangyuyu@superjia.com
 */
(function ($) {
    var shareto = {
        __role: '5',

        init: function (container, options) {
            var self = this
            self.__container = container
            self.__options = options
            self.__file = options.fileItems[0]

            container.html(tpl)

            self.__panel = container.find('.mod-shareto')

            self.__bindEvent()
        },

        __bindEvent: function () {
            var self = this
            var panel = self.__panel

            panel.on('click', '.back', function () {
                $.jps.publish('action-back')
            })

            panel.on('click', '.auth-wrap', function () {
                self.__showAuth()
            })

            panel.on('click', '.auth-item', function() {
                self.__hideAuth()
                var item = $(this)
                self.__role = item.data('role')

                panel.find('.auth-txt').text(item.text())
            })

            var sharetoInput = panel.find('.shareto-input')

            panel.on('click', '.opt-btn', function () {
                var email = $.trim(sharetoInput.val())

                /*if (!email || !App.common.modules.util.isEmail(email)) {
                    App.common.modules.smallnote('请输入正确的电邮地址', {
                        pattern: 'error'
                    })
                    sharetoInput.focus()
                    return false
                } else {*/
                    var fileJson = self.__file.toJSON()
                    $.ajax({
                        url: '/review/file/sharefolder4cjt/',
                        method: 'post',
                        dataType: 'json',
                        data: {
                            path: fileJson.path,
                            shareTO: email + ',0,' + self.__role,
                            reinvite: 0
                        },
                        traditional: true,
                        success: function (data) {
                            if(data.code == '0') {
                                App.common.modules.smallnote('恭喜您，共享成功')
                                $.jps.publish('action-back', true)
                            }
                        }
                    })
                //}
            })
        },

        __showAuth: function () {
            var self = this
            var panel = self.__panel
            var role = self.__role

            panel.find('.auth-item-wrap').show().find('.auth-item[data-role="' + role + '"]').addClass('active').siblings().removeClass('active')
        },

        __hideAuth: function() {
            this.__panel.find('.auth-item-wrap').hide()
        }

    }

    var tpl = '' +
        '<div class="mod-shareto">' +
        '   <div class="action-header">' +
        '       <a class="back">&lt;</a><div class="title">共享</div><div class="opt-btn">确定</div>' +
        '   </div>' +
        '   <div class="shareto-wrap">' +
        '       <div class="email-wrap">' +
        '           <input class="shareto-input" placeholder="用户帐号" />' +
        '       </div>' +
        '   </div>' +
        '   <div class="auth-wrap clr">' +
        '       <div class="desc f-l">访问权</div>' +
        '       <div class="f-r auth-item-btn"><span class="auth-txt">协作</span><span class="gray">&nbsp;&gt;</span></div>' +
        '   </div>' +
        '   <div class="auth-item-wrap">' +
        '       <div class="auth-item" data-role="8">管理员</div>' +
        '       <div class="auth-item" data-role="7">高级协作</div>' +
        '       <div class="auth-item" data-role="5">协作</div>' +
        '       <div class="auth-item" data-role="4">可预览、下载</div>' +
        '       <div class="auth-item" data-role="3">可预览、上传</div>' +
        '       <div class="auth-item" data-role="2">只可上传</div>' +
        '       <div class="auth-item" data-role="1">只可预览</div>' +
        '       <div class="auth-item" data-role="0">禁止访问</div>' +
        '   </div>' +
        '</div>'

    App.modules.sharetoAction = shareto
})(jQuery)
